package com.ruoyi.common.utils;

import java.text.NumberFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Random;

/**
 * @Auther: tswsoft
 * @Date: 2021/03/18/5:30 PM
 * @Description: 单据编码生产，目前规则比较简单，就是YYMMDD 格式的日期 + 系统时间戳 + 随机生产的两位数
 */
public class GenCodeUtils {

    private static int counter=0;

    public static final String genCode(){
        String pre = LocalDateTimeUtils.format(LocalDateTime.now(),LocalDateTimeUtils.PATTERN_YYMMDD);
        LocalDateTime localDateTime = LocalDateTime.of(LocalDateTime.now().getYear(),
                LocalDateTime.now().getMonth(),LocalDateTime.now().getDayOfMonth(),0,0,0);
        Long start = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        Long num = System.currentTimeMillis() - start;

        //得到一个NumberFormat的实例
        NumberFormat nf = NumberFormat.getInstance();
        //设置是否使用分组
        nf.setGroupingUsed(false);
        //设置最大整数位数
        nf.setMaximumIntegerDigits(8);
        //设置最小整数位数
        nf.setMinimumIntegerDigits(8);

        nf.format(num);

        return pre + nf.format(num) + incrementAndGet();
    }

    public static String genCustomCode(int length){
        StringBuilder sb = new StringBuilder();
        String str = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
        String[] strs = str.split("");
        for(int i = 0;i<length;i++){
            sb.append(strs[(int)(Math.random()*strs.length)]);
        }
        return sb.toString();
    }

    /**
     * 随机生成100内的两位数字
     * @return
     */
    public static int incrementAndGet(){
        Random random=new Random();
        int number=random.nextInt(90)+10;
        return number;
    }

    public static void main(String[] args) {
        for(int i=1;i<10;i++){
            System.out.println(genCode());
        }

    }

}
